<!-- 补链操作弹窗 -->
<template>
  <el-dialog
    :title="'补链操作'"
    :visible="visible"
    width="1000px"
    min-hight="500px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="saveForm" :rules="rules" label-width="100px">
      <el-row :gutter="15">
        <el-col :lg="14" :md="14">
          <el-form-item label="游戏账号:" prop="username">
            <el-input
              clearable
              :maxlength="20"
              :disabled="isUpdate"
              v-model="form.username"
              placeholder="请输入游戏账号"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :lg="24" :md="24">
          <el-table
            :data="tableData"
            style="width: 95%; margin-left: 30px; min-height: 60%"
          >
            <el-table-column prop="package_id" label="游戏包id" width="180">
            </el-table-column>
            <el-table-column prop="package_name" label="游戏包名称" width="180">
            </el-table-column>
            <el-table-column prop="server_id" label="区服id" width="180">
            </el-table-column>
            <el-table-column prop="server_name" label="区服名称" width="180">
            </el-table-column>
            <el-table-column prop="role_id" label="角色id" width="180">
            </el-table-column>
            <el-table-column
              prop="promote_account"
              label="所属推广员"
              width="180"
            >
            </el-table-column>
            <el-table-column prop="role_name" label="角色名称" width="180">
            </el-table-column>
            <el-table-column prop="play_time" label="登录时间" width="180">
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :lg="14" :md="14">
          <el-form-item label="游戏项目:" prop="game_id">
            <el-select
              clearable
              v-model="saveForm.game_id"
              placeholder="请选择游戏名称"
              class="ele-fluid"
            >
              <el-option
                v-for="item in gameLists"
                :key="item.id"
                :label="item.game_name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :lg="14" :md="14">
          <el-form-item label="修改前渠道:" prop="username">
            <el-input
              clearable
              :maxlength="20"
              :disabled="isUpdate"
              v-model="beforeModificationChannel"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :lg="14" :md="14">
          <el-form-item label="补链后渠道:" prop="new_promote_id">
            <PageSelect
              :placeholder="'请选择补链后渠道'"
              v-model="saveForm.new_promote_id"
              :whereName="'realname'"
              :url="'/userpromote/index'"
              :optionLable="'realname'"
              :optionvalue="'id'"
              :multiple="false"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :lg="14" :md="14">
          <el-form-item label="备注:" prop="remark">
            <el-input
              type="textarea"
              :rows="3"
              placeholder="请输入内容"
              v-model="saveForm.remark"
            >
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消 </el-button>
      <el-button type="primary" :loading="loading" @click="save"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
import PageSelect from '@/components/pageSelect/index.vue'
export default {
  name: 'MemberFillEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: { PageSelect },
  computed: {
    ...mapGetters(['permission'])
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({ status: 1 }, this.data),
      where: {},
      tableData: [],
      gameLists: [],
      saveForm: {},
      beforeModificationChannel: '',
      // 表单验证规则
      rules: {
        game_id: [
          { required: true, message: '请选择游戏项目', trigger: 'blur' }
        ],
        new_promote_id: [
          { required: true, message: '请选择补链后渠道', trigger: 'blur' }
        ],
        remark: [{ required: true, message: '请填写备注', trigger: 'blur' }]
      },
      showEngineeringEdit: false,
      url: '/member/getMemberGamePromoteInfo',
      selection: [],
      current: null,
      gameEditID: null,
      // 表格列配置

      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    }
  },
  watch: {
    // gameID() {
    //   console.log(this, "ttttthis");
    //   console.log(this.reload, "ttttthisLOAD");
    //   this.reload();
    // },
    visible(val) {
      if (val) {
        this.reload()
        if (this.data) {
          this.form = Object.assign({}, this.data)
          console.log(this.where, this.form.id, 'where')
          this.$set(this.where, 'member_id', this.form.id)
          this.$set(this.saveForm, 'member_id', this.form.id)
          this.$http
            .post('/member/getMemberGamePromoteInfo', this.where)
            .then((res) => {
              this.loading = false
              if (res.data.code === 0) {
                // this.$message.success(res.data.msg);
                this.tableData = res.data.data.gameRoleLists
                this.gameLists = res.data.data.gameLists
                this.reload()
              } else {
                this.$message.error(res.data.msg)
                this.reload()
              }
            })
            .catch((e) => {
              this.loading = false
              this.$message.error(e.message)
              this.reload()
            })
          this.isUpdate = true
        } else {
          this.form = {}
          this.saveForm = {}
          this.isUpdate = false
        }
      }
    },
    'saveForm.game_id': {
      handler(val) {
        console.log(val, 'vvvvvvvvvvvVVVVVVVVVV')
        if (val != undefined) {
          this.getBeforeModificationChannel()
        } else {
          this.saveForm = {}
        }
      }
    }
  },
  methods: {
    /* 刷新表格 */
    reload() {
      // this.saveForm = {};
      this.$refs.table ? this.$refs.table.reload({ where: this.where }) : ''
    },
    getBeforeModificationChannel() {
      console.log(this.saveForm.game_id, 'this.saveForm')
      this.$http
        .post('/member/getMemberGamePromote', {
          game_id: this.saveForm.game_id,
          member_id: this.saveForm.member_id
        })
        .then((res) => {
          this.loading = false
          if (res.data.code === 0) {
            this.beforeModificationChannel = res.data.data
          } else {
            this.$message.error(res.data.msg)
          }
        })
        .catch((e) => {
          this.loading = false
          this.$message.error(e.message)
          this.reload()
        })
    },
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          console.log('!!!!!!!!!', this.saveForm)
          this.loading = true
          this.$http
            .post('/member/setMemberGamePromote', this.saveForm)
            .then((res) => {
              this.loading = false
              if (res.data.code === 0) {
                this.$message.success(res.data.msg)
                this.updateVisible(false)
                this.$emit('done')
                this.saveForm = {}
                this.reload()
              } else {
                this.$message.error(res.data.msg)
                this.reload()
              }
            })
            .catch((e) => {
              this.loading = false
              this.$message.error(e.message)
              this.reload()
            })
        } else {
          return false
        }
      })
    },
    /* 更新visible */
    updateVisible(value) {
      console.log(value, 'vvv')
      this.saveForm = {}
      this.beforeModificationChannel = ''
      this.$emit('update:visible', value)
    }
  }
}
</script>

<style scoped>
.el-row {
  margin-bottom: 20px;
}
</style>
